package writeor.aidraw.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import writeor.aidraw.model.AiImages;
import writeor.aidraw.model.User;
import writeor.aidraw.service.AiImageService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * 商品分类管理控制器类
 */
@Controller
public class AiImageController {

    @Autowired
    private AiImageService aiImageService;
    @RequestMapping(value = "/GiveLike")//like其实是img_path
    public String GiveLike(@RequestParam(value = "like") int like,@RequestParam(value = "img_path") String img_path,HttpServletRequest request) {
        //String img_path = like;//like其实是img_path
        System.out.println("String img_path路径是:"+img_path+"喜欢数="+like);
        //获取原本有多少like
        //用路径名字获取图片的相关信息
        int image_info=aiImageService.give_like(img_path,like+1);


//        System.out.println("现在用的detail ImageDetail路径是:"+image_info.toString());
//
//        //model.addAttribute("img_path",img_path);
//        //model.addAttribute("image_info",image_info);
//        request.setAttribute("img_path",img_path);
//        request.setAttribute("image_info",image_info);
        return "redirect:/";
    }


    @RequestMapping(value = "/manage/image/my_images")//显示自己的图片
    public String my_images( @RequestParam(value = "page", defaultValue = "1") int page,
                                       @RequestParam(value = "count", defaultValue = "6") int count,
                                       HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("USER_SESSION");

        System.out.println("用户为:"+user.getUser_name());
        PageInfo<AiImages> Images=aiImageService.FindImageListWithUser(page,count,user.getUser_name());
        request.setAttribute("Imageslist",Images);
        request.setAttribute("search","flase");//分页的


        return "/manage/my_image_manage";
    }

    @GetMapping("/manage/image/find_images_with_user")//显示自己的图片,带搜索
    public String find_images_with_user(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "count", defaultValue = "6") int count,
                                        HttpSession session, Model model, @RequestParam(value = "find_images") String find_images){
        System.out.println("find_images："+find_images);
        if(find_images==""){
            //搜索为空，重定向
            System.out.println("搜索为空，重定向");
            return "redirect:my_images";
        }
        User user = (User) session.getAttribute("USER_SESSION");
        System.out.println("用户为:"+user.getUser_name());
        PageInfo<AiImages> Imageslist = aiImageService.SearchImageWithUser(page,count,find_images,user.getUser_name());

        model.addAttribute("Imageslist",Imageslist);
        model.addAttribute("search","true");
        return "/manage/my_image_manage";
    }
    //搜索用户
    @GetMapping("/manage/image/find_images")
    public String find_images(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "count", defaultValue = "6") int count,
                              HttpServletRequest request, Model model, @RequestParam(value = "find_images") String find_images){
        System.out.println("find_images："+find_images);
        if(find_images==""){
            //搜索为空，重定向
            System.out.println("搜索为空，重定向");
            return "redirect:getImageList";
        }
        PageInfo<AiImages> Imageslist = aiImageService.FindImageByPrompt(page,count,find_images);
        //request.setAttribute("Imageslist",Imageslist);
        model.addAttribute("Imageslist",Imageslist);
        model.addAttribute("search","true");
        return "/manage/image_manage";
    }
    @RequestMapping(value = "manage/image/getImageList")
    public String getImageListForPage( @RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "count", defaultValue = "6") int count,
                                      HttpServletRequest request) {
        PageInfo<AiImages> Images=aiImageService.FindImageListForPage(page,count);
        request.setAttribute("Imageslist",Images);
        request.setAttribute("search","flase");//分页的
        return "/manage/image_manage";
    }


    /**
     * 更新商品分类
     */
    @PostMapping("manage/image/updateImage108")
    @ResponseBody
    public Map updateImage108(AiImages aiImages) {
        int rows = aiImageService.updateImage108(aiImages);
        Map map=new HashMap<>();
        if(rows >0){
            map.put("result","success");
            map.put("msg","图片信息修改成功");
        }else{
            map.put("result","fail");
            map.put("msg","图片信息修改失败");
        }
        return map;

    }

   // deleteImage108
    /**
     * 删除商品分类信息
     */
    @RequestMapping("manage/image/deleteImage108/{img_path}")
    public String deleteImage108(@PathVariable("img_path") String img_path) {
        int rows = aiImageService.deleteImage108(img_path);
        System.out.println("要删除的图片"+img_path);
        return "redirect:../getImageList";
    }

}
